package com.platform.qujia.service.impl;


import com.platform.qujia.dao.*;
import com.platform.qujia.pojo.ActivityMessagePushRecord;
import com.platform.qujia.service.ActivityMessagePushRecordService;
import com.platform.qujia.service.WxMessageTemplateService;
import com.platform.qujia.utils.redis.RedisCacheUtil;
import com.platform.qujia.utils.weixinapp.WxPushUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("activityMessagePushRecordService")
public class ActivityMessagePushRecordServiceImpl implements ActivityMessagePushRecordService {
    private static Logger logger =(Logger) LoggerFactory.getLogger(ActivityMessagePushRecordService.class);

    @Resource
    private WxPushUtil wxPushUtil;
    @Resource
    private AppUserInfoMapper appUserInfoMapper;
    @Resource
    private WxMessageTemplateService wxMessageTemplateService;
    @Resource
    private CouponMapper couponMapper;
    @Resource
    private ReductionActivityMapper reductionActivityMapper;
    @Resource
    private CustomerCardSettingDetailContentMapper customerCardSettingDetailContentMapper;
    @Resource
    private ActivityMessagePushRecordMapper activityMessagePushRecordMapper;
    @Resource
    private StationMapper stationMapper;
    @Resource
    private RedisCacheUtil redisCacheUtil;
    @Override
    public void sendWx(ActivityMessagePushRecord activityMessagePushRecord) {
        boolean locked = false;
        String key="DistributedLock_"+activityMessagePushRecord.getBusinessName()+activityMessagePushRecord.getBusinessId();
        try {
            locked = redisCacheUtil.lock(key, "locked", 5);
            if(locked){
                logger.info("优惠活动消息推送开始");
                String businessName = activityMessagePushRecord.getBusinessName();
                ActivityMessagePushRecord pushRecord=null;
                if(activityMessagePushRecord.getBusinessId()!=null){
                    if(businessName.equals(ActivityMessagePushRecord.COUPON_ACTIVITY)){
                        pushRecord=activityMessagePushRecordMapper.selectCouponMesPush(activityMessagePushRecord.getBusinessId());
                        if(pushRecord!=null&&pushRecord.getStationName()!=null){
                            if(pushRecord.getStationName().contains("全部油站")&&pushRecord.getEnterpriseId()!=null){
                                String stationName=stationMapper.getStationNameByEnterpriseId(pushRecord.getEnterpriseId());
                                pushRecord.setStationName(stationName);
                            }
                        }
                    }else if(businessName.equals(ActivityMessagePushRecord.REDUCTION_ACTIVITY)){
                        pushRecord=activityMessagePushRecordMapper.selectReductionActivityMesPush(activityMessagePushRecord.getBusinessId());

                    }else if(businessName.equals(ActivityMessagePushRecord.CUSTOMER_CARD_SETTING_DETAIL)){
                        pushRecord=activityMessagePushRecordMapper.selectCustomerCardSettingMesPush(activityMessagePushRecord.getBusinessId());
                    }
                }
                if(pushRecord!=null){
                    Long enterpriseId = pushRecord.getEnterpriseId();
                    if(enterpriseId!=null){
                        //app_user_info station_id 存的是油企id
//                        List<AppUserInfo> appUserInfos = appUserInfoMapper.selectAppUserInfoByStationId(enterpriseId);
//
//                        for (AppUserInfo appUserInfo : appUserInfos) {
//                            if(StringUtils.isNotEmpty(appUserInfo.getBlogOpenid())){
//                                Map<String, String> templateParams = new HashMap<>();
//                                templateParams.put("content", pushRecord.getContent());
//                                templateParams.put("stationName",pushRecord.getStationName());
//                                logger.info("优惠活动-微信模板参数：{}", templateParams);
//                                templateParams.put("openid", appUserInfo.getBlogOpenid()) ;
//                                wxPushUtil.sendTemplateMsg(Integer.valueOf(String.valueOf(pushRecord.getEnterpriseId())), templateParams, WxTemplateEnum.CREATE_ACTIVITY);
//                            }
//                        }
                        wxMessageTemplateService.batchSendActivityNotifyWxTemplateMessage(pushRecord);;
                    }
                }
            }else{
                logger.info("redis未获取锁");
            }
        }finally {
            if(locked){
                redisCacheUtil.unlock(key,"locked");
            }
        }


    }
}
